package org.swimv2.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneLavoro;
import org.swimv2.controller.GestioneLavoroRemote;
import org.swimv2.model.Lavoro;
import org.swimv2.model.Persona;





public class VisualizzaLavoroServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Lavoro l;
	
	public VisualizzaLavoroServlet() {
        super();
    }
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLavoro.JNDI_NAME);
			GestioneLavoroRemote manager = (GestioneLavoroRemote)PortableRemoteObject.narrow(obj,GestioneLavoroRemote.class);
			List <Persona> membri;
			
			String idLavoro=request.getParameter("lavoroTemp");
			l = manager.dettagliLavoro(Integer.parseInt(idLavoro));
			request.setAttribute("lavoroTemp",l);
			membri = manager.getPartecipantiLavoro(idLavoro);
			request.setAttribute("listMembri", membri);
			request.getRequestDispatcher("WEB-INF/paginaLavoro.jsp").
			forward(request, response);	
		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
		
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLavoro.JNDI_NAME);
			GestioneLavoroRemote manager = (GestioneLavoroRemote)PortableRemoteObject.narrow(obj,GestioneLavoroRemote.class);
			//long IDLavoroTemp = Long.parseLong(request.getParameter("IDlavoroTemp"));
			Lavoro lavoroTemp = (Lavoro)request.getAttribute("lavoroTemp");
			request.setAttribute("lavoroTemp", lavoroTemp);
			request.setAttribute("listMembri", lavoroTemp.getPersonas());
			request.getRequestDispatcher("WEB-INF/paginaLavoro.jsp").
			forward(request, response);	
		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
		
	}
	

}
